package com.randi.system.service.impl.sysDict;

import com.github.pagehelper.PageInfo;
import com.randi.common.constant.CacheConstants;
import com.randi.common.enums.ResultCode;
import com.randi.common.exception.ServiceException;
import com.randi.common.utils.SecurityUtils;
import com.randi.system.domain.entity.sysDict.SysDict;
import com.randi.system.domain.entity.sysDict.SysDictData;
import com.randi.system.domain.params.sysDict.SysDictDataParam;
import com.randi.system.mapper.sysDict.SysDictDataMapper;
import com.randi.system.service.sysDict.ISysDictDataService;
import com.randi.system.service.sysDict.ISysDictService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ziye
 */
@Service
@CacheConfig(cacheNames = CacheConstants.SYS_DICT_DATA)
public class SysDictDataServiceImpl implements ISysDictDataService {

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Resource
    private ISysDictService iSysDictService;

    /**
     * 分页查询业务列表
     *
     * @param sysDictDataParam sysDictData
     * @return 业务集合
     */
    @Override
    @Cacheable(keyGenerator = "baseKeyGenerator", unless = "#result == null")
    public PageInfo<SysDictData> selectPageList(SysDictDataParam sysDictDataParam) {
        return new PageInfo<>(sysDictDataMapper.selectSysDictDataList(sysDictDataParam));
    }

    /**
     * 查看详情
     *
     * @return 业务集合
     */
    @Override
    @Cacheable(key = "#dictDataId", unless = "#result == null")
    public SysDictData selectSysDictDataById(Long dictDataId) {
        return sysDictDataMapper.selectSysDictDataById(dictDataId);
    }

    /**
     * 插入数据
     *
     * @param sysDictData sysDictData
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int insertSysDictData(SysDictData sysDictData) {
        Long dictId = sysDictData.getDictId();
        SysDict sysDict = iSysDictService.selectSysDictById(dictId);
        if (sysDict == null || sysDict.getDictId() == null || sysDict.getDeleted() == 1) {
            throw new ServiceException(ResultCode.BUSINESS_INSERT_ERROR_DETAIL, "字典不存在");
        } else if (sysDict.getStatus() == 1) {
            throw new ServiceException(ResultCode.BUSINESS_INSERT_ERROR_DETAIL, "字典已停用");
        }

        SecurityUtils.setInsertInfo(sysDictData);
        return sysDictDataMapper.insert(sysDictData);
    }

    /**
     * 更新数据
     *
     * @param sysDictData SysDictData
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int updateSysDictData(SysDictData sysDictData) {
        Long dataId = sysDictData.getDataId();
        if (dataId == null) {
            throw new ServiceException(ResultCode.PARAM_ERROR);
        }
        sysDictData.setDictId(null);
        SecurityUtils.setUpdateInfo(sysDictData);
        return sysDictDataMapper.updateById(sysDictData);
    }

    /**
     * 批量删除数据
     *
     * @param dictDataIds dictDataIds
     * @return status
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int deleteBatchSysDictData(List<Long> dictDataIds) {
        return sysDictDataMapper.batchUpdateDeleted(dictDataIds);
    }

}
